package com.guigu.www.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.guigu.www.dao.bean.Employee;
import com.guigu.www.dao.mapper.EmployeeMapper;
import com.guigu.www.service.EmployeeService;
import com.guigu.www.service.pojo.EmployeeAndDept;
import com.guigu.www.service.pojo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangChenZhen
 * @since 2021-03-17
 */
@RestController()
public class EmployeeController {
    @Autowired
    EmployeeService service;
    @Autowired
    EmployeeMapper employeeMapper;

    @PostMapping("setAndAddEmp")
    public void setEmployeeAddEmployee(@RequestBody Employee employee){
        System.out.println("employee");
        System.out.println("["+employee.getEmpno()+"]");
        if(employee.getEmpno()==0){
            employeeMapper.insert(employee);
        }else{
            employeeMapper.update(employee,new EntityWrapper<Employee>().eq("empno",employee.getEmpno()));
        }
    }

    @PostMapping("delEmp")
    public void delEmployee(Integer id){
        if(id!=null)
            employeeMapper.deleteById(id);
    }


    @PostMapping("emp")
    public PageVo<EmployeeAndDept> table(@RequestBody Map<String,String> map){
        //分页条件
        Wrapper<Employee> wrapper=new EntityWrapper<Employee>();
        if(map.get("startTime").equals(""))
            map.put("startTime","0");
        if(map.get("endTime").equals(""))
            map.put("endTime","9");
        if(!map.get("sex").equals("0")){
            wrapper=wrapper.eq("job",map.get("sex"));
        }
        if(!map.get("dept").equals("0")){
            wrapper=wrapper.eq("deptno",map.get("dept"));
        }
        wrapper=wrapper.like("ename",map.get("name"));
        wrapper=wrapper.between("hiredate",map.get("startTime"),map.get("endTime"));
        //分页
        PageVo<EmployeeAndDept> pageVo=new PageVo<EmployeeAndDept>();
        Page<EmployeeAndDept> page=service.query(new Page<Employee>(Integer.valueOf( map.get("pageNum")),Integer.valueOf(map.get("pageSize"))),wrapper);
        return new PageVo<EmployeeAndDept>(page.getTotal(),page.getRecords());
    }


}

